#!/bin/bash

# 等待数据库就绪
echo "Waiting for database..."
sleep 2

# 使用 mysql 检查 aerich 表是否存在
mysql -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE -e "SHOW TABLES LIKE 'aerich'" | grep -q aerich
AERICH_EXISTS=$?

if [ $AERICH_EXISTS -ne 0 ]; then
    echo "Initializing Aerich..."
    aerich init -t config.TORTOISE_ORM
    aerich init-db
else
    echo "Running database migrations..."
    aerich upgrade
fi

# 启动应用
exec uvicorn main:app --host 0.0.0.0 --port 8000 